﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using SCG.TurboSprite;
using SCG.SolarVengeanceEngine;
using System.IO;
using SCG.WinForms;

namespace SCG.SolarVengeance
{
    public class StarShipImageFactory
    {
        //Get a colorized basic image of a ship
        public static Bitmap GetBasicImage(StarShipType shipType, Color color)
        {
            List<Image> images = _masterLists[shipType.ToString()];
            Bitmap img = (Bitmap)images[2];
            img = ImageColorizer.Colorize(img, color, Color.Black);
            return img;
        }

        //Get colorized bitmap 
        public static Bitmap GetImage(StarShipType shipType, Color color, int facing)
        {
            List<Image> images = _masterLists[shipType.ToString()];
            Bitmap img = (Bitmap)images[facing];
            img = ImageColorizer.Colorize(img, color, Color.Black);
            return img;
        }

        //Get the unkown ship type image
        public static Bitmap GetUnknown(Color color)
        {
            List<Image> images = _masterLists["Unknown"];
            Bitmap img = (Bitmap)images[2];
            img = ImageColorizer.Colorize(img, color, Color.Black);
            return img;
        }

        //private members
        private static Dictionary<string, List<Image>> _masterLists = new Dictionary<string, List<Image>>();

        //Load an image into the factory
        private static void LoadImage(string name)
        {
            string fileName = @"Images\StarShips\" + name + ".bmp";
            if (!File.Exists(fileName))
                throw new ArgumentException("File does not exist: " + fileName);
            Bitmap bmp = (Bitmap)Bitmap.FromFile(fileName);
            if (bmp.Width != Constants.ShipImageSize || bmp.Height != Constants.ShipImageSize)
                throw new InvalidOperationException("StarShip images must be " + Constants.ShipImageSize + "x" + Constants.ShipImageSize);

            bmp = ImageUtils.Create32bppBitmap(bmp);
            bmp.MakeTransparent(Color.Black);

            //store rotations so we can use Facing as the index
            List<Image> images = new List<Image>();
            Image rotated = ImageUtils.RotateImage(bmp, 270);
            images.Add(rotated);
            rotated = ImageUtils.RotateImage(bmp, 270 + 45);
            images.Add(rotated);
            images.Add(bmp);
            rotated = ImageUtils.RotateImage(bmp, 45);
            images.Add(rotated);
            rotated = ImageUtils.RotateImage(bmp, 90);
            images.Add(rotated);
            rotated = ImageUtils.RotateImage(bmp, 90 + 45);
            images.Add(rotated);
            rotated = ImageUtils.RotateImage(bmp, 180);
            images.Add(rotated);
            rotated = ImageUtils.RotateImage(bmp, 180 + 45);
            images.Add(rotated);

            _masterLists[name] = images;
        }

        //static constructor
        static StarShipImageFactory()
        {
            //create factories for each ship type
            foreach (StarShipType shipType in StarShip.ShipTypes)
                LoadImage(shipType.ToString());
            LoadImage("Unknown");
        }
    }
}
